<template>
  <el-dialog
    :title="title"
    :visible="showDialog"
    width="400px"
    center
    @close="btnCancel"
  >
    <template #title>
      <el-row type="flex" justify="start">
        <span class="title">{{ title }}</span>
      </el-row>
    </template>
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
      <el-form-item v-if="!subjectID" label="所属学科" prop="subjectID">
        <el-select v-model="ruleForm.subjectID" placeholder="请选择">
          <el-option v-for="item in sympleDate" :key="item.value" :label="item.label" :value="item.value" />

        </el-select>
      </el-form-item>
      <el-form-item label="目录名称" prop="directoryName" placeholder="请输入目录名称">
        <el-input v-model="ruleForm.directoryName" />
      </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-row type="flex" justify="end">
        <el-button @click="btnCancel">取 消</el-button>
        <el-button type="primary" @click="btnOk">确 定</el-button>
      </el-row>

    </span>
  </el-dialog>
</template>

<script>
// import { add } from '@/api/hmmm/directorys.js'
export default {
  props: {
    showDialog: {
      type: Boolean,
      require: true
    },
    sympleDate: {
      require: true
    },
    subjectID: {
      require: true
    }
  },
  data() {
    return {
      rules: {
        subjectID: [
          { required: true, message: '请输入所属学科', trigger: 'blur' }
        ],
        directoryName: [
          { required: true, message: '请输入目录名称', trigger: 'blur' }
        ] },
      ruleForm: {
        subjectID: '',
        directoryName: ''
      },
      title: '新增目录'

    }
  },
  created() {
    if (this.subjectID) {
      this.ruleForm.subjectID = this.subjectID
    } else {
      this.ruleForm = {
        subjectID: '',
        directoryName: ''
      }
    }
  },

  methods: {
    btnCancel() {
      this.$refs.ruleForm.resetFields()//   validateField
      this.$emit('update:showDialog', false)
      this.title = '新增目录'
    },
    btnOk() {
      this.$refs.ruleForm.validate(async isOk => {
        if (isOk) {
          if (this.subjectID) {
            this.ruleForm.subjectID = Number(this.subjectID)
          }
          console.log(this.ruleForm)
          this.$emit('addSub', this.ruleForm)// 通知父组件更新信息
        }
      })
    }
  }
}
</script>

<style scoped lang='scss'>
.title{
    color: white;
    font-size: 20px;
}
::v-deep{
.el-input--medium {
    width: 280px;
}
.el-dialog__body{
   padding: 25px 15px 30px
}
.el-form-item__label{
    width: 80px!important;
}
.el-form-item__content{
margin-left: 80px!important;
}
.el-input__inner{
    height: 32px!important;

}
}

</style>
